/**
 * Crazy Table
 *  License by CrazyYi.
 *  v1.0.0 20200206
 *  v1.1.0 20200214
 *  v1.2.0
 *  v1.3.0 20200228: 新增setting属性，单独设置pkey和mark
 *
 *  reference: https://lolicode.gitee.io/tablefilter/
 *  reference: https://saodiyang.gitee.io/layui-crazy-table
 *
 */
const layuiModule = [
    'table',
    'crazyFilter', 'crazyEdit', 'crazyChild',
    'crazyQuery', 'crazyData', 'crazyHttp', 'crazyDate', 'crazyTips',
    'crazyToolbar', 'crazyUpload',
];
layui.define(layuiModule, function (exports) {
    let MOD_NAME = 'crazyTable';
    let $Child = layui.crazyChild,
        $Filter = layui.crazyFilter,
        $Edit = layui.crazyEdit,
        $ = layui.$,
        $tQuery = layui.crazyQuery,
        $Data = layui.crazyData,
        $Http = layui.crazyHttp,
        $Tool = layui.crazyToolbar,
        $Date = layui.crazyDate,
        $Upload = layui.crazyUpload,
        $Tips = layui.crazyTips

    let defaultOptions = {
        v: '1.0.0',
        logOn: false, // 是否开启Log 打印
    };

    let gOptions = {};

    // 定义一个对象的深度克隆方法
    const deepClone = function (obj) {
        // 先检测是不是数组和Object
        // let isArr = Object.prototype.toString.call(obj) === '[object Array]';
        let isArr = Array.isArray(obj);
        let isJson = Object.prototype.toString.call(obj) === '[object Object]';
        if (isArr) {
            // 克隆数组
            let newObj = [];
            $.each(obj, function (i, o) {newObj[i] = deepClone(o)});
            return newObj;
        } else if (isJson) {
            // 克隆Object
            let newObj = {};
            $.each(obj, function (i, o) {newObj[i] = deepClone(o)});
            return newObj;
        }
        // 不是引用类型直接返回
        return obj;
    };
    Object.prototype.deepClone = function () { return deepClone(this); };
    Object.defineProperty(Object.prototype, 'deepClone', {enumerable: false});
    // 给string对象添加原型方法 replaceAll()
    String.prototype.replaceAll = function (s1, s2) { return this.replace(new RegExp(s1, "gm"), s2); };
    Object.defineProperty(String.prototype, 'replaceAll', {enumerable: false}); // 不可枚举展示

    let mod = {

        render: function (myTable, res) {
            console.log('--------- Welcome to crazyTable ---------');
            console.log('>>> table options:', myTable);
            console.log('>>> table done result:', res);

            // 1.加载 预处理模块：crazyQuery, crazyData, crazyHttp
            // 加载 crazyQuery
            $tQuery.render(myTable, res);

            // 加载 crazyData
            // res.pkey = myTable.httpConfig.pkey || {field: 'id', name: 'id'}; // 将pkey 保存到缓存中
            $Data.render(myTable, res);

            // 加载 crazyHttp
            $Http.render(myTable);

            // 2.加载 处理模块：child, edit, filter, toolbar, upload
            $Child.render(myTable); // 子表扩展
            $Edit.render(myTable, res); // 单元格编辑
            $Filter.render(myTable, res); // 表头筛选
            $Tool.render(myTable, res); // toolbar
            $Upload.render(myTable); // upload
            $Tips.render(myTable); // tips
        },
        Http: $Http,
        Data: $Data,
        tQuery: $tQuery,
        Date: $Date,
        Tool: $Tool,
        Upload: $Upload,

        Child: $Child,
        Edit: $Edit,
        Filter: $Filter,

    };

    //输出接口
    exports(MOD_NAME, mod);
});